package com.uenpay.im.ui.old.activity.dynamic;

import android.text.TextUtils;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qiyunxin.android.http.utils.StringUtils;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class PublishDynamicInvokeItem extends HttpInvokeItem {
    public class PublishResult extends HttpInvokeResult {
    }

    public PublishResult getOutput() {
        return (PublishResult) GetResultObject();
    }

    public PublishDynamicInvokeItem(String content,
                                    ArrayList<FileMessage> fileMessages, FileMessage videoFileMessge, FileMessage videoCoverFileMessage) {
        String files = "";
        String video = "";
        String video_cover = "";
        try {
            if (fileMessages != null && fileMessages.size() > 0) {
                JSONArray arr = new JSONArray();
                for (FileMessage fileMessage : fileMessages) {
                    JSONObject picObject = new JSONObject();
                    picObject.put("fileid", fileMessage.fileid);
                    picObject.put("filehash", fileMessage.filehash);
                    arr.put(picObject);
                }
                if (arr != null && arr.length() > 0) {
                    files = arr.toString();
                }
                files = arr.toString();
            }
            if (videoFileMessge != null) {
                //视频
                JSONObject json = new JSONObject();
                json.put("fileid", videoFileMessge.fileid);
                json.put("filehash", videoFileMessge.filehash);
                video = json.toString();
            }
            if (videoCoverFileMessage != null) {
                //视频封面
                JSONObject json = new JSONObject();
                json.put("fileid", videoCoverFileMessage.fileid);
                json.put("filehash", videoCoverFileMessage.filehash);
                video_cover = json.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(files)) {
            files = StringUtils.encodeParams(files);
        }
        if (!TextUtils.isEmpty(content)) {
            content = StringUtils.encodeParams(content);
        }
        if (!TextUtils.isEmpty(video_cover)) {
            video_cover = StringUtils.encodeParams(video_cover);
        }
        if (!TextUtils.isEmpty(video)) {
            video = StringUtils.encodeParams(video);
        }
        // http://api.aitiapp.com/Dynamic/addDynamic?content=&files=&visible=
        String url = APIConfiguration.getBaseUrl()
                + "Dynamic/Dynamic/addDynamic?content=" + content + "&files=" + files + "&visible=2&" + APIConfiguration.getCustIdAndToken() + "&video=" + video + "&video_cover=" + video_cover;
        SetUrl(url.trim());
    }

    @Override
    protected Object DeserializeResult(String responseBody) {
        PublishResult result = new PublishResult();
        JSONObject json = null;
        try {
            json = new JSONObject(responseBody);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (json == null) {
            return null;
        }
        result.status = json.optInt("status");
        result.msg = json.optString("msg");
        return result;
    }
}
